import router from "@/router";
/**
 * 树转数组扁平化结构
 * 深度优先遍历  递归
 */
export default {
  goPage(routerName, params, query, queryKey) {
    if (queryKey) {
      router.push({
        name: routerName,
        params: { id: params },
        query: { [queryKey]: query }
      });
    } else {
      router.push({
        name: routerName,
        params: { id: params },
        query: { info: query }
      });
    }
  },
  // 截取长度为多少的字符串
  str(str, length) {
    var len = 0;
    let newStr = "";
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i);
      //单字节加1
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        len++;
      } else {
        len += 2;
      }
      if (len < length) {
        newStr = newStr + str[i];
      } else {
        return newStr + "...";
      }
    }
    if (len < length) {
      return newStr;
    }
  },
  timer(data) {
    //毫秒转时间
    let times = new Date((data - 0) * 1000);
    let year = times.getFullYear();
    let month = "";
    let day = "";
    let hours = "";
    let minutes = "";
    let seconds = "";
    if (times.getMonth() + 1 <= 9) {
      month = "0" + (times.getMonth() + 1);
    } else {
      month = times.getMonth() + 1;
    }
    if (times.getDate() <= 9) {
      day = "0" + times.getDate();
    } else {
      day = times.getDate();
    }
    if (times.getHours() <= 9) {
      hours = "0" + times.getHours();
    } else {
      hours = times.getHours();
    }
    if (times.getMinutes() <= 9) {
      minutes = "0" + times.getMinutes();
    } else {
      minutes = times.getMinutes();
    }
    if (times.getSeconds() <= 9) {
      seconds = "0" + times.getSeconds();
    } else {
      seconds = times.getSeconds();
    }
    let date =
      year +
      "-" +
      month +
      "-" +
      day +
      " " +
      hours +
      ":" +
      minutes +
      ":" +
      seconds;
    return date;
  }
};
